package org.luxor.eshop.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.eshop.order.entity.dto.CreateOrderDto;
import org.luxor.eshop.order.entity.dto.OrderDto;
import org.luxor.eshop.order.service.OrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 订单服务
 *
 * @author Mr.Yan  @date 2019/9/19
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController extends BaseRestController {
    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PreAuthorize("isAnonymous()")
    @PostMapping("/create")
    public R<OrderDto> create(@RequestBody CreateOrderDto createOrder) {
        OrderDto order = orderService.create(createOrder);
        if (null == order || null == order.getOrderId()) {
            return R.failed("创建订单失败");
        }
        return R.ok(order);
    }

    /**
     * 获取订单
     */
    @PreAuthorize("isAnonymous()")
    @GetMapping("/{orderId}")
    public R<OrderDto> get(@PathVariable Long orderId) {
        OrderDto order = orderService.get(orderId);
        return R.ok(order);
    }

    @PreAuthorize("isAnonymous()")
    @PostMapping("/paySuccess/{orderId}")
    public R paySuccess(@PathVariable Long orderId) {
        orderService.paySuccess(orderId);
        return R.ok();
    }

    @PreAuthorize("isAnonymous()")
    @PostMapping("/payFailed/{orderId}")
    public R payFailed(@PathVariable Long orderId) {
        orderService.payFailed(orderId);
        return R.ok();
    }
}
